package com.jic.veteran.controller;

import cn.hutool.core.util.StrUtil;
import com.google.common.collect.Sets;
import com.jic.veteran.common.api.CommonPage;
import com.jic.veteran.common.api.CommonResult;
import com.jic.veteran.dto.CmsArticleQueryParam;
import com.jic.veteran.dto.CmsArticleUpdateParam;
import com.jic.veteran.model.CmsArticleInfo;
import com.jic.veteran.service.CmsArticleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import java.util.List;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

/**
 * 文章管理Controller
 *
 * @author lsw1991abc@gmail.com
 * @date 2021/6/29 09:47
 */
@RestController
@Api(tags = "CmsArticleController")
@RequestMapping("/article")
public class CmsArticleController {
    @Autowired
    private CmsArticleService articleService;

    private static final Set<String> ARTICLE_STATUS_FLAG_COLLECT = Sets.newHashSet(
            "enable", "disable");

    @ApiOperation("查询文章列表")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public CommonResult<CommonPage<CmsArticleInfo>> list(
            CmsArticleQueryParam queryParam,
            @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize,
            @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum) {
        List<CmsArticleInfo> articleInfoList = articleService.list(queryParam, pageSize, pageNum);
        return CommonResult.success(CommonPage.restPage(articleInfoList));
    }

    @ApiOperation("新增或更新文章信息")
    @RequestMapping(value = "saveOrUpdate", method = RequestMethod.POST)
    public CommonResult<Integer> saveOrUpdate(@RequestBody CmsArticleUpdateParam param) {

        CmsArticleInfo articleInfo = new CmsArticleInfo();
        articleInfo.setId(param.getId());
        articleInfo.setArticleTitle(param.getArticleTitle());
        articleInfo.setIsShow(param.getIsShow());
        articleInfo.setArticleType(param.getArticleType());
        articleInfo.setPreviewImg(param.getPreviewImg());
        articleInfo.setArticleContent(param.getArticleContent());
        int count = articleService.saveOrUpdate(articleInfo);
        return CommonResult.success(count);
    }

    @ApiOperation("批量调整展示状态")
    @RequestMapping(value = "/change/{status}", method = RequestMethod.POST)
    public CommonResult<Integer> changeStatus(
            @RequestBody List<Long> ids, @PathVariable String status) {
        if (StrUtil.isEmpty(status) || !ARTICLE_STATUS_FLAG_COLLECT.contains(status.toLowerCase())) {
            return CommonResult.failed("未知的目标状态");
        }
        int count = articleService.changeStatus(ids, status.toLowerCase());
        return CommonResult.success(count);
    }

    @ApiOperation("批量删除文章")
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult delete(@RequestBody List<Long> ids) {
        int count = articleService.delete(ids);
        if (count > 0) {
            return CommonResult.success(count);
        }
        return CommonResult.failed();
    }

    @ApiOperation("获取文章详情")
    @RequestMapping(value = "/detail/{id}", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<CmsArticleInfo> detail(@PathVariable Long id) {
        CmsArticleInfo articleInfo = articleService.detail(id);
        return CommonResult.success(articleInfo);
    }

}
